package com.cpd.blog.dto;

import com.cpd.blog.common.constant.StatusCode;
import lombok.Getter;
import lombok.Setter;

/**
 * 返回结果
 *
 * @author Copydown
 * @date 2021/8/14 17:09
 */
@Getter
@Setter
public class Result {

    private boolean success;
    /**
     * 操作执行成功的状态码都返回 200
     */
    private int code;

    private String msg;

    private Object data;

    public boolean isSuccess() {
        return success;
    }

    public Result(boolean success, int code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result success() {
        return new Result(true, StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), null);
    }

    public static Result success(Object data) {
        return new Result(true, StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), data);
    }

    public static Result success(StatusCode statusCode) {
        return new Result(true, StatusCode.SUCCESS.getCode(), statusCode.getMsg(), null);
    }

    public static Result fail(int code, String msg) {
        return new Result(false, code, msg, null);
    }

    public static Result fail(StatusCode statusCode) {
        return new Result(false, statusCode.getCode(), statusCode.getMsg(), null);
    }
}
